﻿Imports FairShop
Imports FairShop.Helpers
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports System
Imports System.Text
Imports System.Collections.Generic
Imports System.IO

<TestClass()> _
Public Class TestMeldingen

    Private Const TESTWINKELIER As String = "TESTWINKELIER"

    Private testContextInstance As TestContext

    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = value
        End Set
    End Property

#Region "Additional test attributes"
    '
    ' You can use the following additional attributes as you write your tests:
    '
    ' Use ClassInitialize to run code before running the first test in the class
    <ClassInitialize()> _
    Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
        AppDomain.CurrentDomain.SetData("APPBASE", Environment.CurrentDirectory)
    End Sub
    '
    ' Use ClassCleanup to run code after all tests in a class have run
    ' <ClassCleanup()> Public Shared Sub MyClassCleanup()
    ' End Sub
    '
    ' Use TestInitialize to run code before running each test
    <TestInitialize()> _
    Public Sub MyTestInitialize()
        ' Clean up extra
        ClearTestWinkeliers()

        ' testwinkelier toevoegen (let op: we werken hier op de FairshopOefen.mdb van de lokale FairShop-toepassing!)
        Dim winkelier As ArtikelsDataSet.WinkeliersRow = Utility.Data.Winkeliers.NewWinkeliersRow()
        winkelier.Naam = TESTWINKELIER
        Utility.Data.Winkeliers.AddWinkeliersRow(winkelier)
        Dal.WinkelierToevoegen(winkelier)

        ' meldingen zelf weg (alleen in memory)
        Utility.Data.Meldingen.Clear()

        Security.HuidigeWinkelierNaam = TESTWINKELIER
    End Sub
    '
    ' Use TestCleanup to run code after each test has run
    <TestCleanup()> _
    Public Sub MyTestCleanup()
        ClearTestWinkeliers()
    End Sub

    Public Sub ClearTestWinkeliers()
        Try
            Dim winkeliers As IEnumerable(Of ArtikelsDataSet.WinkeliersRow) = (From w In Utility.Data.Winkeliers _
                                                              Where w.Naam = TESTWINKELIER).ToList()
            For Each winkelier As ArtikelsDataSet.WinkeliersRow In winkeliers
                winkelier.Delete()
            Next
            Dim winkeliersAdapter As New ArtikelsDataSetTableAdapters.WinkeliersTableAdapter()
            winkeliersAdapter.Update(Utility.Data.Winkeliers)
        Catch ex As Exception
            ' inslikken
        End Try
        Utility.ResetData()  ' zodat eventuele overgebleven wijzigingen niet doorgaat naar andere tests
    End Sub

#End Region

    <TestMethod()> _
    Public Sub TestEersteMeldingGetoondEersteKeer()
        AddMeldingInMemory("test1", 1)
        Assert.IsTrue(MeldingenHelper.MoetMeldingGetoondWorden(), "Eerste melding moet getoond worden")
    End Sub

    <TestMethod()> _
    Public Sub TestEersteMeldingNietGetoondTweedeKeer()
        AddMeldingInMemory("test1", 1)
        Assert.AreEqual(1, MeldingenHelper.GetAndUpdateTeTonenMeldingId(), "Verkeerde id van melding")
        Assert.IsFalse(MeldingenHelper.MoetMeldingGetoondWorden(), "Eerste melding mag niet meer getoond worden")
    End Sub

    <TestMethod()> _
    Public Sub TestTweedeMeldingGetoondEersteKeer()
        AddMeldingInMemory("test1", 1)
        AddMeldingInMemory("test2", 2)
        Assert.AreEqual(1, MeldingenHelper.GetAndUpdateTeTonenMeldingId(), "Verkeerde id van melding")
        Assert.IsTrue(MeldingenHelper.MoetMeldingGetoondWorden(), "Tweede melding moet getoond worden")
        Assert.AreEqual(2, MeldingenHelper.GetAndUpdateTeTonenMeldingId(), "Verkeerde id van melding")
    End Sub

    Private Shared Sub AddMeldingInMemory(ByVal tekst As String, ByVal id As Integer)
        Dim melding As ArtikelsDataSet.MeldingenRow = Utility.Data.Meldingen.NewMeldingenRow()
        melding.Melding = tekst
        melding.Id = id
        Utility.Data.Meldingen.AddMeldingenRow(melding)
        Utility.Data.Meldingen.AcceptChanges()
    End Sub

End Class
